package leihua.fifter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Author leiHua
 * @Date 2022/11/16 13:29
 */
@WebFilter("/*")
public class MyFilter implements Filter {
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        String referer = request.getHeader("referer");
        System.out.println("referer="+referer);
        if (referer == null || !referer.contains(request.getServerName())) {
            response.sendRedirect("/error.jsp");
            System.out.println("访问失败");
        } else {
            filterChain.doFilter(request, response);
        }

    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }
    public void destroy() {

    }
}
